﻿Imports System.Xml
Public Class ChampsTree
    Dim fermes As New List(Of Ferme)
    Private Sub TreeView1_AfterSelect(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TreeViewEventArgs)

    End Sub

    Private Sub CollectionBindingSource_CurrentChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)

    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.Text = "Repérage"
        Me.Width = Screen.PrimaryScreen.Bounds.Width
        Me.Height = Screen.PrimaryScreen.Bounds.Height
        Panel1.Top = 10
        Panel1.Left = 10
        buildTree()
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

    End Sub
    Private Sub buildTree()
        Try
            Dim xmlDoc As New XmlDocument
            xmlDoc.Load("\PFAMobile\champs.xml")
            Dim racineXML As XmlNode
            Dim racineNode As New TreeNode()
            Dim i As Long
            i = 0
            TreeView1.Nodes.Clear()
            racineXML = xmlDoc.DocumentElement
            'MsgBox(racineXML.Name)
            If racineXML.Name.ToString.Equals("Champs") Then
                racineNode.Text = racineXML.Name
                For Each ferme As XmlNode In racineXML.ChildNodes
                    Dim fNode As New TreeNode()
                    Dim secteurs As New List(Of Secteur)
                    For Each secteur As XmlNode In ferme.ChildNodes
                        If TypeOf secteur Is XmlText Then
                            fNode.Text = secteur.Value 'Il s'agit du nom de la ferme
                        Else
                            Dim scNode As New TreeNode()
                            Dim serres As New List(Of Serre)
                            For Each serre As XmlNode In secteur.ChildNodes
                                If TypeOf serre Is XmlText Then
                                    scNode.Text = serre.Value 'Il s'agit là du nom du secteur
                                Else
                                    Dim srNode As New TreeNode()
                                    Dim billons As New List(Of Billon)
                                    For Each billon As XmlNode In serre.ChildNodes
                                        If TypeOf billon Is XmlText Then
                                            srNode.Text = billon.Value 'Nom de la serre
                                        Else
                                            Dim bNode As New TreeNode()
                                            bNode.Text = billon.FirstChild.Value 'Nom du billon
                                            srNode.Nodes.Add(bNode)
                                            Dim b As New Billon
                                            b.idChamp = Val(billon.Attributes("id").Value)
                                            b.nomChamp = bNode.Text
                                            billons.Add(b)
                                        End If
                                    Next
                                    scNode.Nodes.Add(srNode)
                                    Dim sr As New Serre
                                    sr.billons = billons
                                    sr.idChamp = Val(serre.Attributes("id").Value)
                                    sr.nomChamp = srNode.Text
                                    serres.Add(sr)
                                End If
                            Next
                            Dim st As New Secteur
                            st.serres = serres
                            st.idChamp = Val(secteur.Attributes("id").Value)
                            st.nomChamp = scNode.Text
                            secteurs.Add(st)
                            fNode.Nodes.Add(scNode)
                        End If
                    Next
                    'MsgBox(ferme.Attributes("id").Value)
                    'MsgBox(fNode.Text)
                    Dim f As New Ferme()
                    f.idChamp = Val(ferme.Attributes("id").Value)
                    f.nomChamp = fNode.Text
                    f.secteurs = secteurs
                    fermes.Add(f)
                    racineNode.Nodes.Add(fNode)
                Next
                TreeView1.Nodes.Add(racineNode)
            Else
            End If
        Catch ex As Exception
            MsgBox(ex.Message.ToString)
            Exit Sub
        End Try
    End Sub

    Private Sub TreeView1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs)
        'loadChampLocation()
    End Sub

    Private Sub loadChampLocation()
        Dim formChamp As New ChampLocation(getChampFromDB(TreeView1.SelectedNode.Text))
        'Me.Hide()
        formChamp.Show()
    End Sub

    Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        MsgBox(TreeView1.SelectedNode.Text)
        loadChampLocation()
    End Sub

    Public Function getChampFromDB(ByVal ch As String) As Champ
        For Each f As Ferme In fermes
            If f.nomChamp = ch Then
                Return f
                'Exit Function
            Else
                For Each st As Secteur In f.secteurs
                    If st.nomChamp = ch Then
                        Return st
                        'Exit Function
                    Else
                        For Each sr As Serre In st.serres
                            If sr.nomChamp = ch Then
                                Return sr
                                'Exit Function
                            Else
                                For Each b As Billon In sr.billons
                                    If b.nomChamp = ch Then
                                        Return b
                                        'Exit Function
                                    End If
                                Next
                            End If
                        Next
                    End If
                Next
            End If
        Next
        Return Nothing
    End Function
End Class
